<?php

	class Datasekolah extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model('m_data_sekolah');
			$this->load->model('frontmodel');
		}
		
		public function index()
		{		
			$data['title']	= '| Data Sekolah';
			$data['main'] 	= 'admin/legalisir/datasekolah/v_datasekolah';
			$this->load->view('v_template',$data); 
		}
		
		public function json_datasekolah()
		{
			$data = $this->m_data_sekolah->get();
			
			echo '{ "geonames": [';
			
			$i = 0;
			foreach($data as $row)
			{
				$status = $row['STATUS'];
				if($status == 'N'){
					$status = '"status" : "Negeri"';
				}elseif($status == 'S'){
					$status = '"status" : "Swasta"';
				}elseif($status == 'NF'){
					$status = '"status" : "Non-Formal"';
				}
				
				if($status == NULL || $status == '')
					$status = '"status" : "-"';
				
				$i++;
				if($i >= count($data))
				{
					echo '{
						"npsn" 		: "'.$row['NPSN'].'" ,
						"nama"		: "'.$row['NAMA'].'" ,
						"alamat"	: "'.$row['ALAMAT'].'" ,
						"jenjang"	: "'.$row['JENJANG'].'",
						'.$status.',
						"button" : "<a href=\''.site_url('admin/datasekolah/edit/'.$row['NPSN']).'\' title=\'Edit Data\' class=\'btn btn-info btn-xs\'><i class=\'fa fa-pencil-square-o\'></i></a>"
					}';
				}
				else
				{
					echo '{
						"npsn" 		: "'.$row['NPSN'].'" ,
						"nama"		: "'.$row['NAMA'].'" ,
						"alamat"	: "'.$row['ALAMAT'].'" ,
						"jenjang"	: "'.$row['JENJANG'].'" ,
						"jenjang"	: "'.$row['JENJANG'].'" ,
						'.$status.',
						"button" : "<a href=\''.site_url('admin/datasekolah/edit/'.$row['NPSN']).'\' title=\'Edit Data\' class=\'btn btn-info btn-xs\'><i class=\'fa fa-pencil-square-o\'></i></a>"
					},';
				}
			}
			echo ']}';
		}
		
		public function add()
		{
			$this->form_validation->set_rules('nama','Nama Sekolah','required');
			$this->form_validation->set_rules('alamat','Alamat','required');
			
			if($this->form_validation->run() == FALSE)
			{
				$data['title']	= '';
				$data['main'] 	= 'admin/legalisir/datasekolah/add_datasekolah';
				$this->load->view('v_template',$data);
			}
			else
			{
				$data = array(
					'NPSN'		=> $this->input->post('npsn'),
					'NAMA'		=> $this->input->post('nama'),
					'ALAMAT'	=> $this->input->post('alamat'),
					'JENJANG'	=> $this->input->post('jenjang'),
					'STATUS'	=> $this->input->post('status')
				);
				
				$this->m_data_sekolah->save($data);
				
				redirect('admin/datasekolah');
			}
		}
		
		public function edit($id = FALSE)
		{
			$this->form_validation->set_rules('nama','Nama Sekolah','required');
			$this->form_validation->set_rules('alamat','Alamat','required');
			
			if($this->form_validation->run() == FALSE)
			{
				$data['title']		= '';
				$data['get_data']	= $this->m_data_sekolah->get($id);
				$data['main'] 		= 'admin/legalisir/datasekolah/edit_datasekolah';
				$this->load->view('v_template',$data);
			}
			else
			{
				$data = array(
					'NPSN'		=> $this->input->post('npsn'),
					'NAMA'		=> $this->input->post('nama'),
					'ALAMAT'	=> $this->input->post('alamat'),
					'JENJANG'	=> $this->input->post('jenjang'),
					'STATUS'	=> $this->input->post('status')
				);
				
				$this->m_data_sekolah->save($data,$id);
				
				redirect('admin/datasekolah');
			}
		}
	}